package com.yiwei.ydd.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.ydd.R;
import com.yiwei.ydd.activity.WebViewActivity;
import com.yiwei.ydd.api.model.CashBackListsModel;
import com.yiwei.ydd.api.model.ExpandPacketModel;
import com.yiwei.ydd.util.DateTime;
import com.yiwei.ydd.util.Dialog;
import com.yiwei.ydd.util.Spans;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xsooy-pc on 18-4-9.
 */


public class ExpandPacketAdapter extends BaseExpandableListAdapter {

    private final int x26;
    private final int x22;
    private List<CashBackListsModel.DatasBean> gData;

    private Context mContext;

    public ExpandPacketAdapter(List<CashBackListsModel.DatasBean> gData, Context mContext) {
        this.gData = gData;
        this.mContext = mContext;
        x26 = mContext.getResources().getDimensionPixelSize(R.dimen.x26);
        x22 = mContext.getResources().getDimensionPixelSize(R.dimen.x22);
    }

    public void add(List<CashBackListsModel.DatasBean> items, boolean init) {
        if (init) {
            gData.clear();
            gData = items;
        } else {
            gData.addAll(items);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return gData.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return gData.get(groupPosition).items.size();
    }

    @Override
    public CashBackListsModel.DatasBean getGroup(int groupPosition) {
        return gData.get(groupPosition);
    }

    @Override
    public CashBackListsModel.DatasBean.ItemsBean getChild(int groupPosition, int childPosition) {
        return gData.get(groupPosition).items.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    //取得用于显示给定分组的视图. 这个方法仅返回分组的视图对象
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        ViewHolderGroup groupHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_group_packet, parent, false);
            groupHolder = new ViewHolderGroup();
            groupHolder.txtContent = (TextView) convertView.findViewById(R.id.txt_content);
            groupHolder.txtStatus = (TextView) convertView.findViewById(R.id.txt_status);
            groupHolder.btn_tips = (ImageView) convertView.findViewById(R.id.btn_tips);
            groupHolder.layoutTitle = (LinearLayout) convertView.findViewById(R.id.layout_title);
            convertView.setTag(groupHolder);
        } else {
            groupHolder = (ViewHolderGroup) convertView.getTag();
        }

        CashBackListsModel.DatasBean item = getGroup(groupPosition);


        //订单状态（-2：失效，-1：失败，0：待支付，1：完成，2：已付款，3：分期种）
        String status = "";
        switch (item.status) {
            case "-2":
                status = "失效";
                break;
            case "-1":
                status = "失败";
                break;
            case "0":
                status = "待支付";
                break;
            case "1":
                status = "完成";
                break;
            case "2":
                status = "已付款";
                break;
            case "3":
                status = "分期中";
                break;

        }
        groupHolder.txtStatus.setText(status);

        Spanned name = Spans.with(mContext).font(item.name, x26, 0xff666666).
                font("（剩余" + item.last_back + "元未到账）", x22, 0xffFB5E01).
                build();
        groupHolder.txtContent.setText(name);
        if (isExpanded) {
            groupHolder.txtStatus.setTextColor(0xffFB5E01);
            groupHolder.layoutTitle.setVisibility(View.VISIBLE);
            groupHolder.btn_tips.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Dialog.showDefaultDialog(mContext, "", "到账总额为加油套餐充值每月返现+加油包每个月返现的总额", "", "明白了", false, new Dialog.DialogDefaultClickListener() {
                        @Override
                        public void confirm() {

                        }

                        @Override
                        public void cancel() {

                        }
                    });
                }
            });

        } else {
            groupHolder.layoutTitle.setVisibility(View.GONE);
            groupHolder.txtStatus.setTextColor(0xff999999);
        }
        return convertView;
    }

    //取得显示给定分组给定子位置的数据用的视图
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolderItem itemHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_child_packet, parent, false);
            itemHolder = new ViewHolderItem();

            itemHolder.txtMoney = (TextView) convertView.findViewById(R.id.txt_money);
            itemHolder.txtStatus = (TextView) convertView.findViewById(R.id.txt_status);
            itemHolder.txtTime = (TextView) convertView.findViewById(R.id.txt_time);

            convertView.setTag(itemHolder);
        } else {
            itemHolder = (ViewHolderItem) convertView.getTag();


            CashBackListsModel.DatasBean.ItemsBean item = getChild(groupPosition, childPosition);
            itemHolder.txtTime.setText(DateTime.toDate(Long.valueOf(item.get_sdate) * 1000));
            //分期状态（-1：失败，1：完成，2：待领取）
            String status = "";
            switch (item.status) {
                case "-1":
                    status = "失败";
                    itemHolder.txtStatus.setTextColor(0xff999999);
                    break;
                case "1":
                    status = "已到账";
                    itemHolder.txtStatus.setTextColor(0xffFB5E01);
                    break;
                case "2":
                    status = "未到账";
                    itemHolder.txtStatus.setTextColor(0xff999999);
                    break;
            }
            itemHolder.txtStatus.setText(status);
            itemHolder.txtMoney.setText(item.money + "(" + item.period + "/" + item.periods + ")");
        }


        return convertView;
    }


    //设置子列表是否可选中
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


    private static class ViewHolderGroup {
        private TextView txtContent;
        private TextView txtStatus;
        private ImageView btn_tips;
        private LinearLayout layoutTitle;
    }

    private static class ViewHolderItem {

        private TextView txtMoney;
        private TextView txtTime;
        private TextView txtStatus;

    }

}